package cdu.notFound.Dao.impl;
import cdu.notFound.Dao.UserDao;
import cdu.notFound.model.User;
import cdu.notFound.utils.DatabaseConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
	private Connection conn = null;
	private PreparedStatement pstmt = null ;

	//获取mysql连接
	public UserDaoImpl(){
		try {
			this.conn=new DatabaseConnection().getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建一个用户
	 * @param user
	 * @return
	 */
	public boolean doCreate(User user){
		boolean flag = false ;
		try {
			String sql = "INSERT INTO user(email,pwd,name,phone) VALUES (?,?,?,?)" ;
			this.pstmt = this.conn.prepareStatement(sql) ;
			this.pstmt.setString(1,user.getEmail()) ;
			this.pstmt.setString(2,user.getPwd()) ;
			this.pstmt.setString(3,user.getName()) ;
			this.pstmt.setString(4,user.getPhone()) ;
			if(this.pstmt.executeUpdate() > 0){
				flag = true ;
			}
			this.pstmt.close() ;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return flag ;
	}

	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	public boolean doUpdate(User user){
		boolean flag = false ;
		String sql = "update user set pwd=?,name=?,phone=?,img=?,address=? where id=?" ;
		try {
			this.pstmt = this.conn.prepareStatement(sql) ;
			this.pstmt.setString(1,user.getPwd());
			this.pstmt.setString(2,user.getName()) ;
			this.pstmt.setString(3,user.getPhone()) ;
			this.pstmt.setString(4,user.getImg()) ;
			this.pstmt.setString(5,user.getAddress()) ;
			this.pstmt.setInt(6,user.getId()) ;
			if(this.pstmt.executeUpdate() > 0){
				flag = true ;
			}
			this.pstmt.close() ;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return flag ;
	}

	/**
	 * 消息为空
	 * @param user
	 * @return
	 */
	public boolean emptyMessnum(User user){
	    String sql = "update user set mess_num=0 WHERE id="+user.getId();
		try {
			this.pstmt = this.conn.prepareStatement(sql) ;
			if(this.pstmt.executeUpdate(sql)>0){
				user.setMessnum(0);
				return true;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	    return false;
	}

	/**
	 * 根据id查找User
	 * @param id
	 * @return
	 */
	public User findById(int id){
		User user = null ;
		String sql = "SELECT id,email,pwd,name ,phone,mess_num,img,address FROM user WHERE id=?" ;
		try {
			this.pstmt = this.conn.prepareStatement(sql) ;
			this.pstmt.setInt(1,id) ;
			ResultSet rs = this.pstmt.executeQuery();
			if(rs.next()){
				user = new User() ;
				user.setId(rs.getInt(1)) ;
				user.setEmail(rs.getString(2)) ;
				user.setPwd(rs.getString(3)) ;
				user.setName(rs.getString(4)) ;
				user.setPhone(rs.getString(5)) ;
				user.setMessnum(rs.getInt(6)) ;
				user.setImg(rs.getString(7)) ;
				user.setAddress(rs.getString(8));
			}
		rs.close();this.pstmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return user ;
	}

	/**
	 *
	 * 根据email查找user
	 * @param
	 * @return
	 */
	public User findByEmail(String email){
		User user = null;
		try {
			String sql = "SELECT id,email,pwd,name,phone,mess_num,img,address FROM user WHERE email=?";
			this.pstmt = this.conn.prepareStatement(sql);
			this.pstmt.setString(1, email);
			ResultSet rs = this.pstmt.executeQuery();
			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt(1));
				user.setEmail(rs.getString(2));
				user.setPwd(rs.getString(3));
				user.setName(rs.getString(4));
				user.setPhone(rs.getString(5));
				user.setMessnum(rs.getInt(6));
				user.setImg(rs.getString(7));
				user.setAddress(rs.getString(8));
			}
			rs.close();
			this.pstmt.close();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return user;
	}
	
	public void close(){
		if(this.conn != null){
			try{
				this.conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}